import router from './router'
import store from './store'
import * as ThreeStorage from '@/utils/store/token.js'
import { isWechat, setDocumentTitle } from '@/utils/isJudge'
import Utils from '@/utils/util'

const whiteList = ['/companySignup', '/driverSignup', '/signup/success', '/signin/normal', '/signin/sms', '/error',
  '/noPerm', '/notFound', '/bidSignin/bidSigniNormal'] // 不重定向白名单
router.beforeEach((to, from, next) => {
  typeof to.meta.title !== undefined && setDocumentTitle(to.meta.title)
  // 检测是否是微信端浏览
  const isFlag = false
  console.log('isWechat()', isWechat())
  if (isFlag) {
    // 对token 授权做准备----微信端
    if (to.matched.some(record => record.meta.requiresAuth)) {
      console.log('isLogin()', ThreeStorage.isLogin())
      if (ThreeStorage.isLogin()) {
        if (to.path === '/signin/normal') {
          if (ThreeStorage.isHaveLoginType()) {
            const loginType = ThreeStorage.getLoginType()
            if (ThreeStorage.getRolesVal()) {
              router.push({ name: 'bid-home' })
            } else {
              if (loginType === 'company') {
                router.push({ name: 'me-adminLanding' })
              } else {
                router.push({ name: 'me-driverLanding' })
              }
            }
          } else {
            store.dispatch('FedLogOut')
          }
        } else {
          // 登录成功时即 进行 路由权限初始化，然后存储当前userkey登录可以使用的路由,
          // 此时 取出当前用户 可用路由列表，与当前 路由name进行比对 赋值isMatched，来判断是否能进入此路径页面
          let add_routers = []
          if (ThreeStorage.getRouterList().length !== 0) {
            add_routers = ThreeStorage.getRouterList()
          } else {
            store.dispatch('InitRouter')
            add_routers = ThreeStorage.getRouterList()
          }
          const isMatched = Utils.isHavePathName(add_routers, to.name)
          // 判断访问的地址是否在allrouter中
          if (isMatched === true) {
            console.log('isMatched === true')
            if (to.path === '/me/driverLanding') {
            	// 微信公众号改菜单后 此处需要改 /me/driverLanding
            	if (ThreeStorage.isHaveLoginType()) {
            		const loginType = ThreeStorage.getLoginType()
                if (ThreeStorage.getRolesVal()) {
                  router.push({ name: 'bid-home' })
                } else {
                  if (loginType === 'company') {
                    router.push({ name: 'me-adminLanding' })
                  } else {
                    next()
                  }
                }
            	} else {
            		store.dispatch('FedLogOut')
            	}
            }
            next()
          } else {
            console.log('isMatched === false')
            router.push({ name: 'noPerm' })
          }
        }
      } else {
			  console.log('to.path', to.path)
        if (to.path === '/signin/normal') {
        	next()
        } else {
        	if (whiteList.indexOf(to.path) !== -1) {
        		next()
        	} else {
        		router.push({ name: 'signup-normal' })
        	}
        }
      }
    } else {
      next()
    }
  } else {
    // 对token 授权做准备----非微信端
    if (to.matched.some(record => record.meta.requiresAuth)) {
      if (ThreeStorage.isLogin()) {
        if (to.path === '/signin/normal') {
          if (ThreeStorage.isHaveLoginType()) {
            const loginType = ThreeStorage.getLoginType()
            if (ThreeStorage.getRolesVal()) {
              router.push({ name: 'bid-home' })
            } else {
              if (loginType === 'company') {
                router.push({ name: 'me-adminLanding' })
              } else {
                router.push({ name: 'me-driverLanding' })
              }
            }
          } else {
            store.dispatch('FedLogOut')
          }
        } else {
          // 登录成功时即 进行 路由权限初始化，然后存储当前userkey登录可以使用的路由,
          // 此时 取出当前用户 可用路由列表，与当前 路由name进行比对 赋值isMatched，来判断是否能进入此路径页面
          let add_routers = []
          if (ThreeStorage.getRouterList().length !== 0) {
            add_routers = ThreeStorage.getRouterList()
          } else {
            store.dispatch('InitRouter')
            add_routers = ThreeStorage.getRouterList()
          }
          const isMatched = Utils.isHavePathName(add_routers, to.name)
          // 判断访问的地址是否在allrouter中
          if (isMatched === true) {
            console.log('isMatched === true')
            if (to.path === '/me/driverLanding') {
              // 微信公众号改菜单后 此处需要改 /me/driverLanding
              if (ThreeStorage.isHaveLoginType()) {
                const loginType = ThreeStorage.getLoginType()
                if (ThreeStorage.getRolesVal()) {
                  router.push({ name: 'bid-home' })
                } else {
                  if (loginType === 'company') {
                    router.push({ name: 'me-adminLanding' })
                  } else {
                    next()
                  }
                }
              } else {
                store.dispatch('FedLogOut')
              }
            }
            next()
          } else {
            console.log('isMatched === false')
            router.push({ name: 'noPerm' })
          }
        }
      } else {
        if (to.path === '/signin/normal') {
          next()
        } else {
          if (whiteList.indexOf(to.path) !== -1) {
            next()
          } else {
            router.push({ name: 'signup-normal' })
          }
        }
      }
    } else {
      next()
    }
  }
})
